<script setup>
import { ref } from "vue";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { userloginStore } from "@/stores/islogin.js";
import { des_phone } from "@/utils/desensitization";
import { useUserStore } from "@/stores/user.js";

onShow(() => {
  if (user.userInfo) {
    userlogin.setlogin(true);
    getuserInfogfun();
  } else {
    userinfo.value = {
      name: user.userInfo.name,
      nickname: user.userInfo.nickname,
      phone: user.userInfo.phone,
      level: user.userInfo.level,
      avatar: user.userInfo.avatar,
      referrer: user.userInfo.referrer,
    };
  }

  referrer_id.value = user.userInfo.referrer_id ? user.userInfo.referrer_id : 0;
});
const referrer_id = ref(0);
const user = useUserStore();
const userlogin = userloginStore();
const userinfo = ref(null);

async function getuserInfogfun() {
  // try {
  let res;
  res = await user.getuserInfo();
  if (res.code == 200) {
    userlogin.setlogin(true);
  }
  userinfo.value = {
    name: res.name,
    nickname: res.nickname,
    phone: res.phone,
    level: res.level,
    avatar: res.avatar,
    referrer: res.referrer,
    referrer_id: res.referrer_id,
  };
  referrer_id.value = userinfo.value.referrer_id
    ? userinfo.value.referrer_id
    : 0;
}

// 去更改推荐人
const goReferee = () => {
  if (referrer_id.value == 1) {
    uni.navigateTo({
      url: "/pages/mine/modifySupplier",
    });
  } else {
    uni.showToast({
      title: "推荐人不是系统，无法修改！",
      icon: "none",
    });
    return;
  }
};

const callPhone = (phoneNumber) => {
  // 判断手机是否支持拨打电话功能
  if (uni.canIUse("makePhoneCall")) {
    uni.makePhoneCall({
      phoneNumber: phoneNumber, // 电话号码
      success: function () {},
      // fail: function () {
      //   uni.showToast({
      //     title: "拨打电话失败",
      //     icon: "error",
      //   });
      // },
    });
  } else {
    uni.showToast({
      title: "你的设备不支持拨打电话功能",
      icon: "none",
    });
  }
};
</script>

<template>
  <view class="content">
    <view class="user-ghs" v-if="(userinfo && referrer_id == 1)">
      <image src="../../../static/my/icon-user.png" mode="widthFix" />
      <text
        >我的推荐人：{{ userinfo ? userinfo.referrer.nickname : "---" }}</text
      >
      <view class="select-ghs" @click="goReferee">更改推荐人</view>
    </view>

    <view class="my-info" v-if="userinfo && referrer_id > 1">
      <view class="my-info-sf">
        <image src="../../../static/my/icon-user1.png" mode="scaleToFill" />
        <text>我的推荐人：</text>
        <text class="my-info-sf-right">{{
          userinfo ? userinfo.referrer.nickname : "---"
        }}</text>
      </view>
      <view class="my-info-sf">
        <image src="../../../static/my/icon-ghs.png" mode="scaleToFill" />
        <text>推荐人 ID：</text>
        <text class="my-info-sf-right">{{
          userinfo ? userinfo.referrer.uid : "---"
        }}</text>
        <up-copy :content="userinfo ? userinfo.referrer.uid : '---'">
          <view class="my-info-sf-click"> 复制 </view>
        </up-copy>
      </view>
      <view class="my-info-sf">
        <image src="../../../static/my/icon-phone.png" mode="scaleToFill" />
        <text>推荐人手机：</text>
        <text class="my-info-sf-right">{{
          des_phone(userinfo ? userinfo.referrer.phone : "---")
        }}</text>
        <text
          class="my-info-sf-click"
          @click="callPhone(userinfo.referrer.phone)"
          >拨打</text
        >
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@import "index.scss";
</style>
